import XEUtils from 'xe-utils'
import axios from '@/d2admin/plugin/axios'
import BaseApi, { PageParams } from '@/module/boot/base.api'
import { CUDBuilder, QueryBuilder } from '@/plugins/apijson'
import Condition from '@/plugins/apijson/condition'
/**
 * 权限信息
 */
class ApiJsonRequestApi extends BaseApi {
  public page(pageParams: PageParams) {
    const tag = XEUtils.isUndefined(pageParams.params.tag)
      ? ''
      : pageParams.params.tag
    return axios.post(
      '/get',
      QueryBuilder.by('Request')
        .multi(true)
        .page(pageParams.page, pageParams.limit)
        .buildCondition('tag', 'like', tag)
        .toJson()
    )
  }
  /**
   * 保存
   * @param model 待保存的模型
   */
  save(model: any) {
    return axios.post(
      '/post',
      CUDBuilder.by('Request')
        .setData(model.Request)
        .setTag('Request')
        .toJson()
    )
  }
  /**
   * 待删除的模型
   * @param model 待保存的模型
   */
  deleteBatch(models: Array<any>) {
    const ids = XEUtils.map(models, item => item.Request.id)
    console.log(ids)
    return axios.post(
      '/delete',
      CUDBuilder.by('Request')
        .setTag('Request')
        .whereId('in', ids)
        .toJson()
    )
  }
  /**
   * 待修改的模型
   * @param model 待保存的模型
   */
  update(model: any) {
    return axios.post(
      '/put',
      CUDBuilder.by('Request')
        .setTag('Request')
        .setData(model.Request)
        .whereId('eq', model.Request.id)
        .toJson()
    )
  }
}
export default new ApiJsonRequestApi()
